﻿using Kugar.Core.ExtMethod;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using Kugar.Core.Configuration;

namespace Kugar.Tool.DataSync.Client.Filters
{
    public class GlobalFilterAttribute:ActionFilterAttribute
    {
        private static string _appID = "";

        static GlobalFilterAttribute()
        {
            _appID = CustomConfigManager.Default.AppSettings.GetValueByName("AppID");
        }

        public  override async Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            var appid = actionContext.Request.GetRouteData().Values.TryGetValue("appid").ToStringEx();

            if (!appid.CompareTo(_appID,true))
            {
                actionContext.Response.Content=new HttpMessageContent(new HttpResponseMessage(HttpStatusCode.Unauthorized));
                return;
            }
            
            await base.OnActionExecutingAsync(actionContext, cancellationToken);
        }
    }
}
